import entity.Person;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import java.util.List;

public class HibetnateTest {
    public static void main(String[] args) {
        // 加载配置类，进行配置
        Configuration configuration = new Configuration().configure();
        // 根据配置对象获取sessionFactory
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        // 获取session
        Session session = sessionFactory.openSession();

        // 开启事务
        Transaction transaction = session.beginTransaction();

        // 新建实体对象
        //Contact contact = new Contact();
        //contact.setId(7);
        //contact.setNotes("huangshi");
        //contact.setName(new Name("zhou","kangan"));
        //contact.setStarred(true);
        //contact.getPhones().add("17611115962");
        //contact.getPhones().add("17622225962");
        //contact.getPhones().add("17633335962");
        //保存对象，对象的数据会通过注解配置或xml配置信息持久化到数据库表中
        //直接操作对象，不再是操作数据字段
        //session.save(contact);

        //hqlQuery(session);

        //获取builder
        CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();

        //创建查询criteria (类似于HQL,SQL)
        CriteriaQuery<Person> objectQuery = criteriaBuilder.createQuery(Person.class);
        Root<Person> root = objectQuery.from(Person.class);
        objectQuery.select(root);

        //传递Criteria 给session.createQuery
        List<Person> personList = session.createQuery(objectQuery).list();
        for (Person p: personList
             ) {
            System.out.println(p);
        }

        CriteriaQuery<String> singleQuery = criteriaBuilder.createQuery(String.class);
        Root<Person> root1 = singleQuery.from(Person.class);
        singleQuery.select(root1.get("name"));
        singleQuery.where(criteriaBuilder.equal(root1.get("salary"),"3800"));
        //传递Criteria 给session.createQuery
        List<String> stringList = session.createQuery(singleQuery).list();
        for (String p: stringList
        ) {
            System.out.println(p);
        }

        // 事务提交
        transaction.commit();
        // 关闭session
        session.close();
        sessionFactory.close();
    }

    private static void hqlQuery(Session session) {
        String selectQuery = "select p.name,p.id from Person p where p.salary > :salary";
        Query<Object[]> query = session.createQuery(selectQuery, Object[].class)
                .setTimeout(5)
                .setParameter("salary", "3800");
        List<Object[]> result = query.list();
        for (Object[] p:result) {
            System.out.println(p[0]+"--->"+p[1]);
        }

        //查询单一数据
        String singleQuery = "select p.name from Person p where p.salary > :sa";

        Query<String> query2 = session.createQuery(singleQuery, String.class)
                .setParameter("sa", "3800");

        String name = query2.uniqueResult();
        System.out.println(name);

        //查询对象
        String ObjectQuery = "from Person";

        Query<Person> query1 = session.createQuery(ObjectQuery,Person.class).setTimeout(2);

        List<Person> persons = query1.list();
        for (Person p: persons) {
            System.out.println(p);
        }
    }
}
